package www.ym.com.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.R;
import www.ym.com.common.RL;
import www.ym.com.dto.DishDto;
import www.ym.com.entity.DishFlavor;
import www.ym.com.service.DishFlavorService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dishFlavor")
public class DishFlavorController {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增口味
     *
     * @param dishFlavor
     * @return
     */
    @PostMapping("/add")
    public R<String> addFlavor(@RequestBody DishFlavor dishFlavor) {
        dishFlavorService.save(dishFlavor);
        return R.success("", "新增成功");
    }

    /**
     * 修改口味
     *
     * @param dishFlavor
     * @return
     */
    @PostMapping("/update")
    public R<String> updateFlavor(@RequestBody DishFlavor dishFlavor) {
        dishFlavorService.updateById(dishFlavor);
        return R.success("", "修改成功");
    }

    /**
     * 删除口味
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public R<String> delFlavor(Long id) {
        dishFlavorService.removeById(id);
        return R.success("", "删除成功");
    }

    /**
     * 查询列表
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/list")
    public RL<DishFlavor> listFlavor(@RequestBody JSONObject jsonObject) {
        Page<DishFlavor> page = dishFlavorService.pageFlavor(jsonObject);
        List<DishFlavor> records = page.getRecords();
        long total = page.getTotal();
        long pageSize = page.getSize();
        long pageNum = page.getCurrent();
        return RL.listSuccess(records, null, total, pageNum, pageSize);
    }

    /**
     * 菜品中显示的口味选择
     *
     * @return
     */
    @PostMapping("/flavors")
    public R<JSONArray> list() {
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(DishFlavor::getDishId);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        JSONArray flavorList = new JSONArray();
        list.stream().map((item) -> {
            Map<String, Object> flavors = new HashMap<>();
            flavors.put("id", item.getId());
            flavors.put("name", item.getName());
            flavors.put("value", item.getValue());
            flavorList.add(flavors);
            return flavorList;
        }).collect(Collectors.toList());
        return R.success(flavorList,null);
    }

    /**
     * 客户端选择菜品的口味
     * @return
     */
    @GetMapping("/realFlavors")
    public R<List<DishFlavor>> realFlavors(Long id){
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        return R.success(list,"");
    }



}
